#!/bin/bash

source "/root/common"

remote_raw_url="https://gitee.com/magein/shell-utils/raw/master/dockerfile"
# 默认保存路径
default_save_path="/usr/local/docker/dockerfile"
# 保存路径
base_save_path=""
server=""
version=""
eval set -- "$(getopt -q -o "ho:y" -l help,output:,server:,version: -- "$@")"
while [ -n "$1" ]; do
  case "$1" in
  -h | --help)
    echo "Usage:"
    usageFormat " -h,--help" "Get Help"
    usageFormat " -y" "Confirm"
    usageFormat " -o,--output" "Dockerfile Sava Path"
    usageFormat " --server" "Appoint Server"
    usageFormat " --version" "Appoint Server Version"
    exit 0
    ;;
  -y)
    base_save_path="$default_save_path"
    shift 1
    ;;
  -o | --output)
    base_save_path="$2"
    shift 2
    ;;
  --server)
    server="$2"
    shift 2
    ;;
  --version)
    version=$2
    shift 2
    ;;
  --)
    break
    ;;
  esac
done

config_json="/root/.dockerfile.json"
if [ ! -f "$config_json" ]; then
  echo '没有找到/root/.config.json配置文件信息'
  exit 1
fi

servers=($(jsonKeys "$config_json"))
if [ ${#servers[@]} -eq 0 ]; then
  echo '没有找到/root/.config.json配置文件中的keys'
  exit 1
fi

if [ -z "$base_save_path" ]; then
  read -r -e -p "请指定下载路径(默认$default_save_path):" parameter
  base_save_path="$default_save_path"
  if [ -n "$parameter" ]; then
    base_save_path="$parameter"
  fi
fi

if [ -z "$server" ]; then
  i=1
  for server in "${servers[@]}"; do
    echo " $i. $server"
    i=$(("$i" + 1))
  done

  read -r -p "请选择您要下载dockerfile文件: " index

  index=$(("$index" - 1))

  server=${servers[$index]}
fi

server_version=($(jsonKeys "$config_json" ".$server.version"))
if [ ${#server_version[@]} -eq 0 ]; then
  echo "支持的服务:${servers[*]}"
  printError "E:没有找到对应的服务信息"
  exit 1
fi

files=()
if [ -z "$version" ]; then
  i=1
  for version in "${server_version[@]}"; do
    echo " $i. $version"
    i=$(("$i" + 1))
  done
  read -r -p "请选择您要下载的版本: " index
  index=$(("$index" - 1))
  if [ $index -ge 0 ]; then
    version=${server_version[$index]}
  fi

  # 获取版本对应的文件列表
  files=($(jsonKeys "$config_json" ".$server.files.\"$version\""))
else
  i=0
  for item in "${server_version[@]}"; do
    if [ "$item" = "$version" ]; then
      files=($(jsonKeys "$config_json" ".$server.files.\"$version\""))
      break
    fi
    i=$(("$i" + 1))
  done
fi

# 没有获取到对应的版本文件，则取基础的文件
base_files=()
if [ ${#files[@]} -eq 0 ]; then
  # 取base中的文件列表
  i=0
  base_files=($(jsonKeys "$config_json" ".$server.base"))
fi

if [[ ${#files[@]} -eq 0 && ${#base_files[@]} -eq 0 ]]; then
  echo "没有找到要下载文件信息"
  exit 1
fi

filepath="$base_save_path/$server/$version"

echo ""
echo "Download dockerfile info:"
echo ""
printf "%+8s: %-15s\n" "filepath" "$filepath"
printf "%+8s: %-15s\n" "server" "$server"
printf "%+8s: %-15s\n" "version" "$version"
printf "%+8s: %-15s\n" "files" ""
for file in "${files[@]}"; do
  printf "%+9s%-20s\n" "" "$file"
done

while true; do
  if [ -d "$filepath" ]; then
    break
  fi

  if sudo mkdir -p "$filepath"; then
    break
  fi

  i=$(("$i" + 1))
  if [ $i -gt 5 ]; then
    printf "%+9s%s\n" "error: " "$filepath mkdir fail"
    exit 1
  fi
done

echo ""
echo "Downloading:"
files[${#files[@]}]="dockerfile"
if [ ${#files[@]} -gt 0 ]; then
  for filename in "${files[@]}"; do
    savename="$filepath/$filename"
    savepath=${savename%/*}
    if [ ! -d "$savepath" ]; then
      if sudo mkdir -p "$savepath"; then
        printf "%+8s%-40s\n" "error: " "$savepath fail"
        exit 1
      fi
    fi

    wget -q -O "$savename" "$remote_raw_url/$server/$version/$filename"
    if [ ! -f "$savename" ]; then
      printf "%+8s%-40s\n" "error: " "$savename download fail"
    fi
    content=$(cat "$savename")
    if [ -z "$content" ]; then
      printf "%+8s%-40s\n" "error: " "$savename download content empty"
    else
      chmod 755 "$savename"
      printf "%+14s : %-30s\n" "success" "$filename"
    fi
  done
fi

if [ ${#base_files[@]} -gt 0 ]; then
  for filename in "${base_files[@]}"; do
    savename="$filepath/$filename"

    savepath=${savename%/*}
    if [ ! -d "$savepath" ]; then
      if sudo mkdir -p "$savepath"; then
        printf "%+8s%-40s\n" "error: " "$savepath fail"
        exit 1
      fi
    fi

    sudo wget -q -O "$savename" "$remote_raw_url/$server/$version/$filename"
    if [ ! -f "$savename" ]; then
      printf "%+8s%-40s\n" "error: " "$savename download fail"
    fi
    content=$(cat "$savename")
    if [ -z "$content" ]; then
      printf "%+8s%-40s\n" "error: " "$savename download content empty"
    else
      chmod 755 "$savename"
      printf "%+14s : %-30s\n" "success" "$filename"
    fi
  done
fi
